﻿#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<ctype.h>
#include"tree.h"

char menu(void);
void addpet(Tree * pt);
void droppet(Tree * pt);
void showpets(const Tree * pt);
void findpet(const Tree * pt);
void printitem(Item item);
void uppercase(char * str);

int main(){
	tree pets;
	char choice;
	InitializeTree(&pets);
	while ((choice = menu()) != 'q')
	{
		switch (choice)
		{
		case 'a':addpet(&pets);
			break;
		case 'l':showpets(&pets);
			break;
		case'f':findpet(&pets);
			break;
		case'd':droppet(&pets); 
			break;
		case'n':printf("%d prts in club\n", TreeItemCount(&pets));
			break;
		default:puts("Switching Error!");
		}
	}
	DeleteAll(&pets);
	puts("BYE~");
	system("pause");
	return 0;
}
char menu(void){
	char ch;
	puts("Hekaba Pet Club Membership Program");
	puts("Enter the letter corresponding to your choice:");
	puts("a) add a pet                l)show list of pets");
	puts("n) number of pets         f)fing a pet");
	puts("q) quit");
	while ((ch=getchar())!=EOF)
	{
		while (getchar() != '\n')
			continue;
		ch = tolower(ch);
		if (strchr("alrfndq", ch) == NULL)
			puts("Please input an a,l,f,n,d,q:");
		else
			break;
	}
	if (ch == EOF)
		ch = 'q';
	return ch;
}
void addpet(Tree * pt)
{
	Item temp;
	if (TreeIsFull(pt))
		puts("No room in the club.");
	else
	{
		puts("Please enter name of pet:");
		gets(temp.petname);
		puts("Please enter pet kind:");
		gets(temp.ptekind);
		uppercase(temp.petname);
		uppercase(temp.ptekind);
		AddItem(&temp, pt);
	}
}
void droppet(Tree * pt){
	Item temp;
	if (TreeIsEmpty(pt)){
		puts("No enteries.");
		return;
	}
	else
	{
		puts("Please enter name of pet:");
		gets(temp.petname);
		puts("Please enter pet kind:");
		gets(temp.ptekind);
		uppercase(temp.petname);
		uppercase(temp.ptekind);
		if (DeleteItem(&temp, pt))
			printf("%s is dropped from the club.\n", temp.petname);
		else
			printf("%s is not a member.\n", temp.petname);
	}
}
void showpets(const Tree * pt){
	if (TreeIsEmpty(pt))
		puts("No enteries!");
	else
		Traverse(pt, printitem);
}
void findpet(const Tree * pt){
	Item temp;
	if (TreeIsEmpty(pt))
	{
		puts("No enteries.");
		return;
	}
	puts("Please enter name of pet you want to find:");
	gets(temp.petname);
	puts("Please enter pet kind");
	gets(temp.ptekind);
	uppercase(temp.petname); 
	uppercase(temp.ptekind);
	printf("%s the % s", temp.petname, temp.ptekind);
	if (InTree(&temp, pt))
		printf(" is a member.\n");
	else
		printf(" is not a member.\n"); 
}
void printitem(Item item)
{
	printf("Pet : %-19s kind : %-19s\n", item.petname, item.ptekind);
}
void uppercase(char * str){
	while (*str)
	{
		*str = toupper(*str);
		str++;
	}

}
